package jim.coly.web.controller;

import jim.coly.web.domain.User;
import jim.coly.web.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/user-list")
	public String page_list(
			@RequestParam(value = "page", required = false, defaultValue = "0") int page,
			@RequestParam(value = "size", required = false, defaultValue = "10") int size,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sortField,
			Model model) {

		Sort sort = null;
		Page<User> users = null;
		if ("id".equals(sortField)) {
			sort = new Sort(Direction.DESC, "id");
		}

		Pageable pageInfo = new PageRequest(page, size, sort);
		users = userService.findAllUsersByPage(pageInfo);
		model.addAttribute("users", users);

		return "/user_list";
	}

}
